package org.richin.array.util;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Iterator that is used for arrays.
 *
 * @author Copyright &copy; 2008
 * <a href="mailto:javelotinfo@users.sourceforge.net">Frederic Daoud</a>
 */
public class ArrayIterator<T> implements Iterator<T>
{
    // The array over which to iterate.
    private T[] m_array;
    private int m_index;
    private int m_total;

    public ArrayIterator(T[] p_array)
    {
        m_array = p_array;
        m_total = p_array.length;
    }

    /**
     * Returns <code>true</code> if the iteration has more elements.
     *
     * @return <code>true</code> if the iteration has more elements.
     */
    public boolean hasNext()
    {
        return m_index < m_total;
    }

    /**
     * Returns the next element in the iteration of the array.
     *
     * @return the next element in the iteration.
     *
     * @exception NoSuchElementException thrown if this method is called and the
     * iteration is past the end of the array.
     */
    public T next()
    throws NoSuchElementException
    {
        if (m_index < m_total)
        {
            T result = m_array[m_index];
            m_index++;
            return result;
        }
        throw new NoSuchElementException("Iteration is past end of the array");
    }

    /**
     * Throws a {@link java.lang.UnsupportedOperationException}.
     */
    public void remove()
    {
        throw new UnsupportedOperationException("No remove() in array iterator");
    }
}

